package com.realone.packman.codec.decoder;

import com.realone.packman.codec.decoder.vo.BmsDecoderDataVo1c;
import com.realone.packman.codec.encoder.vo.BmsEncoderDataVo06;
import com.realone.packman.codec.encoder.vo.BmsEncoderDataVo07;
import com.realone.packman.codec.exception.BmsDecoderException;

import java.nio.ByteBuffer;

/**
 * 获取BMS保护参数 解码
 *
 * @author hugx
 * @create 2018-09-18 15:16
 */
public class BmsDecoder1c extends BmsDecoder<BmsDecoderDataVo1c> implements BmsDecoderStrategy {


    public BmsDecoder1c(Integer classType) {
        super.classType = classType;
    }

    @Override
    public BmsDecoderDataVo1c decoderBody(ByteBuffer bb) {
        if (bb == null) {
            throw new BmsDecoderException(new NullPointerException());
        }

        BmsDecoderDataVo1c result = new BmsDecoderDataVo1c(super.classType);

        BmsEncoderDataVo06 bmsEncoderDataVo06 = result.getBmsEncoderDataVo06();
        if (bmsEncoderDataVo06 == null) {
            bmsEncoderDataVo06 = new BmsEncoderDataVo06();
            result.setBmsEncoderDataVo06(bmsEncoderDataVo06);
        }
        /*  电池过放电保护电压 */
        bmsEncoderDataVo06.setUnderVoltage(bb.getShort() / 1000.0);

        /*电池过放电恢复电压*/
        bmsEncoderDataVo06.setUnderVoltageRestore(bb.getShort() / 1000.0);

        /*电池放电保护电流*/
        bmsEncoderDataVo06.setDischargeOverCurrent((short) (bb.getShort() / 10.0));

        /*电池短路保护电流*/
        bmsEncoderDataVo06.setDischargeShortCurrent((short) (bb.getShort() / 10.0));

        /*电池放电高温门限*/
        bmsEncoderDataVo06.setDischargeHighTemperature(bb.get());

        /*电池放电低温门限*/
        bmsEncoderDataVo06.setDischargeLowTemperature(bb.get());

        BmsEncoderDataVo07 bmsEncoderDataVo07 = result.getBmsEncoderDataVo07();
        if (bmsEncoderDataVo07 == null) {
            bmsEncoderDataVo07 = new BmsEncoderDataVo07();
            result.setBmsEncoderDataVo07(bmsEncoderDataVo07);
        }

        /*电池充电保护电压 单位V，默认4.25*/
        bmsEncoderDataVo07.setOverVoltage(bb.getShort() / 1000.0);

        /*电池过充电恢复电压*/
        bmsEncoderDataVo07.setOverVoltageRestore(bb.getShort() / 1000.0);

        /*电池充电保护电流*/
        bmsEncoderDataVo07.setChargeOverCurrent((short) (bb.getShort() / 10.0));

        /*电池充电失衡电流*/
        bmsEncoderDataVo07.setChargeUnbalanceCurrent((short) (bb.getShort() / 10.0));

        /*电池充电失衡电压*/
        bmsEncoderDataVo07.setChargeUnbalanceVoltage(bb.getShort() / 1000.0);

        /*电池充电高温门限*/
        bmsEncoderDataVo07.setChargeHighTemperature(bb.get());

        /*电池充电低温门限*/
        bmsEncoderDataVo07.setChargeLowTemperature(bb.get());

        /*电池充电均衡启动电压*/
        bmsEncoderDataVo07.setChargeBalanceVoltage(bb.getShort() / 1000.00);

        /*电池过充电均衡停止电压*/
        bmsEncoderDataVo07.setChargeBalanceendVoltage(bb.getShort() / 1000.00);

        return result;
    }



}
